package com.common.page;

import java.io.Serializable;
import java.util.List;

/**
* @ClassName: Page
* @Description: 分页bean
* @author 冰上浮云
* @date 2013年12月15日 下午1:54:36
*
*/ 
public class Page implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 每页默认10条数据
	 */ 
	protected int pageSize = 15; //每页记录数
	protected int currentPage = 1; // 当前页
	protected int totalPage = 0; // 总页数
	protected int total = 0; // 总数据数
	protected int pageStartRow = 0; // 每页的起始行数
	protected int pageEndRow = 0; // 每页显示数据的终止行数
	protected boolean pagination=false;   //是否分页
	boolean hasNextPage = false; // 是否有下一页
	boolean hasPreviousPage = false; // 是否有前一页
	protected String pagedView; // 用于页面显示
	
	Object obj;   //参数对象与返回对象
	@SuppressWarnings("rawtypes")
    List rows; // 返回的结果

	public Page(int rows, int pageSize) {
		this.init(rows, pageSize);
	}

	public Page() {

	}

	/**
	 * 初始化分页参数:需要先设置totalRows
	 * 
	 */

	public void init(int rows, int pageSize) {

		this.pageSize = pageSize;

		this.total = rows;

		if ((total % pageSize) == 0) {
		    totalPage = total / pageSize;
		} else {
		    totalPage = total / pageSize + 1;
		}

	}
	 public void init(int rows, int pageSize,int currentPage ) {

			this.pageSize = pageSize;

			this.total = rows;

			if ((total % pageSize) == 0) {
			    totalPage = total / pageSize;
			} else {
			    totalPage = total / pageSize + 1;
			}
	        if(currentPage!=0)
				gotoPage(currentPage);	      
		}
	/**
	 * 计算当前页的取值范围：pageStartRow和pageEndRow
	 * 
	 */
	private void calculatePage() {
		if ((currentPage - 1) > 0) {
			hasPreviousPage = true;
		} else {
			hasPreviousPage = false;
		}

		if (currentPage >= totalPage) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}

		if (currentPage * pageSize < total) { // 判断是否为最后一页
			pageEndRow = currentPage * pageSize;
			pageStartRow = pageEndRow - pageSize;
		} else {
			pageEndRow = total;
			pageStartRow = pageSize * (totalPage - 1);
		}
		
	}
	
	/**
	 * 直接跳转到指定页数的页面
	 * 
	 * @param page
	 */
	public void gotoPage(int page) {

		currentPage = page;

		calculatePage();
	}

	public String getPagedView() {
		return pagedView;
	}
	public void setPageView(String pageView){
		this.pagedView=pageView;
	}
	/**
	 * @return
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @return
	 */
	public boolean isHasNextPage() {
		return hasNextPage;
	}

	/**
	 * @return
	 */
	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	/**
	 * @return
	 */
	public int getPageEndRow() {
		return pageEndRow;
	}

	/**
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @return
	 */
	public int getPageStartRow() {
		return pageStartRow;
	}


	/**
	 * @param i
	 */
	public void setCurrentPage(int i) {
		currentPage = i;
	}

	/**
	 * @param b
	 */
	public void setHasNextPage(boolean b) {
		hasNextPage = b;
	}

	/**
	 * @param b
	 */
	public void setHasPreviousPage(boolean b) {
		hasPreviousPage = b;
	}

	/**
	 * @param i
	 */
	public void setPageEndRow(int i) {
		pageEndRow = i;
	}

	/**
	 * @param i
	 */
	public void setPageSize(int i) {
		pageSize = i;
	}

	/**
	 * @param i
	 */
	public void setPageStartRow(int i) {
		pageStartRow = i;
	}


	public boolean isPagination() {
		return pagination;
	}

	public void setPagination(boolean pagination) {
		this.pagination = pagination;
	}

    public int getTotalPage()
    {
        return totalPage;
    }

    public void setTotalPage(int totalPage)
    {
        this.totalPage = totalPage;
    }

    @SuppressWarnings("rawtypes")
    public List getRows()
    {
        return rows;
    }

    public void setRows(@SuppressWarnings("rawtypes") List rows)
    {
        this.rows = rows;
    }

    public int getTotal()
    {
        return total;
    }

    public void setTotal(int total)
    {
        this.total = total;
        
        if ((total % pageSize) == 0) {
            totalPage = total / pageSize;
        } else {
            totalPage = total / pageSize + 1;
        }
    }
    
    
}
